import {
  _decorator,
  AnimationClip,
  Color,
  Component,
  Node,
  Sprite,
  SpriteFrame,
  UITransform,
  Animation,
} from "cc";
import { Communicate } from "../../Test1/Comunicate/Communicate";
import { EventType, WarroirStates } from "../../Test1/Enums/EnumPool";
import { AnimEvents3 } from "../Animation3/AnimEvents3";
import { STEADY_VALUE } from "../../Test1/Kits/SteadyValues";
import { Kit } from "../../Test1/Kits/Kit";
import { InputKey3 } from "../KeyEvent3/InputKey3";
import { AnimState3 } from "../Animation3/AnimState3";
const { ccclass, property } = _decorator;

@ccclass("Test3")
export class Test3 extends Component {
  idleAssetsFolder: string = "warrior/idle";
  attackAssetsFolder: string = "warrior/attack";
  playerNode3: Node;
  comm: Communicate;
  animTest3: AnimEvents3;
  spritesFramesArray: SpriteFrame[];

  async getSpriteFrames(assetPath: string) {
    const kit = Kit.getInstance();
    let spriteFrames = await kit.loadImages(assetPath);
    return spriteFrames;
  }

  async productAnim() {
    const idleState = await this.produceIdleState();
    this.animTest3.animMap.set(WarroirStates.WARRIOR_IDLE, idleState);

    const attackState = await this.produceAttackState();
    this.animTest3.animMap.set(WarroirStates.WARRIOR_ATTACK, attackState);

    this.animTest3.structuredAnimations();
  }

  async produceIdleState() {
    let framesArr = await this.getSpriteFrames(this.idleAssetsFolder);

    let idleClipDuration = 0.3;
    let idleClipName = this.idleAssetsFolder;
    let idleFrameRate = 16;
    let idleState = new AnimState3(
      idleFrameRate,
      idleClipDuration,
      undefined,
      idleClipName,
      framesArr
    );

    return idleState;
  }

  async produceAttackState() {
    let framesArr = await this.getSpriteFrames(this.attackAssetsFolder);

    let attackClipDuration = 0.4;
    let attackClipName = this.attackAssetsFolder;
    let attackClipWrapMode = AnimationClip.WrapMode.Normal;
    let attackState = new AnimState3(
      undefined,
      attackClipDuration,
      attackClipWrapMode,
      attackClipName,
      framesArr
    );
    return attackState;
  }

  ctrlByBtn(data?: string) {
    console.log("ctrlByBtn", data);
    this.animTest3.stateTransition(data[0]);
  }

  async surfaceSetting() {
    const spriteComponent = this.playerNode3.getComponent(Sprite);
    const transform = this.playerNode3.getComponent(UITransform);

    const newColor = new Color(255, 255, 255);
    spriteComponent.color = newColor;

    // 设置宽高
    transform.setContentSize(
      STEADY_VALUE.FIRST.WIDTH,
      STEADY_VALUE.FIRST.HEIGHT
    );

    this.spritesFramesArray = await this.getSpriteFrames(this.idleAssetsFolder);
    spriteComponent.spriteFrame = this.spritesFramesArray[0];
  }

  bindInputKey() {
    let inputKey = this.playerNode3.getComponent(InputKey3);
    inputKey.registerMethod(this.playerNode3);
  }

  initializationNode() {
    let stageTest2Node = new Node("StageTest2Node");
    this.playerNode3 = new Node("playerNode3");

    stageTest2Node.addChild(this.playerNode3);
    this.node.addChild(stageTest2Node);

    this.playerNode3.addComponent(Animation);
    this.playerNode3.addComponent(UITransform);
    this.playerNode3.addComponent(Sprite);
    this.playerNode3.addComponent(InputKey3);

    this.playerNode3.addComponent(AnimEvents3);
    this.animTest3 = this.playerNode3.getComponent(AnimEvents3);
  }

  start() {
    this.surfaceSetting();
    this.productAnim();
    this.bindInputKey();
  }

  protected onLoad(): void {
    this.initializationNode();
    this.comm = Communicate.getInstance;
    this.comm.on(EventType.IDENTIFY_SPRINT, this.ctrlByBtn, this);
  }
}
